package com.lion.uiwithzhihu.fragments;




import java.util.ArrayList;
import java.util.List;

import com.activeandroid.Model;
import com.activeandroid.query.Select;
import com.lion.model.Story;
import com.lion.uiwithzhihu.R;
import com.lion.uiwithzhihu.WebViewActivity;
import com.lion.uiwithzhihu.utils.DButils;
import com.lion.uiwithzhihu.utils.ImageUtils;
import com.lion.uiwithzhihu.utils.PreferenceUtils;
import com.xinbo.utils.UILUtils;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MyFragmentCollect extends Fragment
{
	private ArrayList<Story> list =new ArrayList<>();
	private MyAdapter myAdapter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View layout = inflater.inflate(R.layout.myfragment_collect,null);
		ListView mListView = (ListView) layout.findViewById(R.id.collect_listview1);
		myAdapter = new MyAdapter();
		//initData();
		mListView.setAdapter(myAdapter);
		return layout;
	}
	private void initData()
	{
		//List<Story> stories = new Select().from(Story.class).execute();
		List<Story> queryCollect = DButils.queryCollect();
		list.clear();
		list.addAll(queryCollect);
		Log.e("onCreateView",list.toString());
		myAdapter.notifyDataSetChanged();
		
	}
	class MyAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return 0;
		}

		@SuppressLint("ViewHolder")
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View layout = getActivity().getLayoutInflater().inflate(R.layout.myfragment_collect_list_item,null);
			TextView mTextView = (TextView) layout.findViewById(R.id.tv_textview_collect);
			ImageView mImageView = (ImageView) layout.findViewById(R.id.iv_imageview_collect);
			Story story = list.get(position);
			mTextView.setText(story.getTitle());
			Log.e("onCreateView",story.toString()+"");
			boolean isPic = PreferenceUtils.getNoPic(getContext());
			List<String> images = story.getImages();
			if (images==null||images.size()==0)
			{
				mImageView.setVisibility(View.GONE);
			}else {
				mImageView.setVisibility(View.VISIBLE);
				if (isPic)
				{
					ImageUtils.displayImage(getContext(), images.get(0), mImageView);
				}else {
					UILUtils.displayImage(images.get(0), mImageView);
				}	
			}
			final int index =position;
			layout.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View v)
				{
					Story story = list.get(index);
					Intent intent = new Intent(getActivity(),WebViewActivity.class);
					intent.putExtra("story", story);
					startActivity(intent);
					
				}
			});
			return layout;
		}
		
	}
	@Override
	public void onStart()
	{
		initData();
		super.onStart();
	}
	
	
}
